import store from '@/store'
import useConfirm from '@/hooks/useConfirm'
import { InspectionTaskModel, InspectResultkModel } from '@/models'
const inspectionTaskModel = new InspectionTaskModel()
const inspectResultk = new InspectResultkModel()

const getDefaultState = () => {
    return {
        detailBaseInfo: {},
        flawlRecord: {}
    }
}

const state = getDefaultState()

const getters = {}

const mutations = {
    setDetailBaseInfo(state, detailBaseInfo) {
        state.detailBaseInfo = detailBaseInfo
    },
    setFlawlRecordRecord(state, flawlRecord) {
        state.flawlRecord = flawlRecord
    }
}

const actions = {
    async add({ commit, getters }, data) {
        const res = await useConfirm({
            text: '是否确定保存表单',
            model: inspectResultk,
            modelAction: 'add',
            data
        })
        return res
    },
    async detail({ commit }, id) {
        try {
            store.commit('openLoading')
            const res = await inspectionTaskModel.detail({ id }, 'filter')
            commit('setDetailBaseInfo', inspectResultk.transformNull(res))
            return res
        } finally {
            store.commit('closeLoading')
        }
    },
    async getTaskRecord({ commit }, data) {
        try {
            store.commit('openLoading')
            const res = await inspectResultk.getTaskRecord(data)
            const transformRes = inspectResultk.transformNull(res.list)
            commit('setFlawlRecordRecord', transformRes)
            return res
        } finally {
            store.commit('closeLoading')
        }
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}
